package neusoft.yuejian_11.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.MatchBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.MatchModel;
import neusoft.yuejian_11.R;

public class MatchDetailActivity extends BaseActivity implements RetrofitListener {
    protected Context context;
    private int matchId;
    private String matchName;
    private String matchPhone;
    private String matchPicture;
    private TextView textName;
    private ImageButton imageButtonBack;
    private ImageView imageView;
    private TextView textInfo;
    private TextView textAddress;
    private TextView textPrice;
    private TextView textTime;
    private TextView textPhone;
    private TextView textNumber;
    private Button buttonMatchn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match_detail);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        textName = (TextView) findViewById(R.id.textView_match_name);
        imageView = (ImageView) findViewById(R.id.imageView);
        textPhone = (TextView) findViewById(R.id.text_match_phone);

        textAddress = (TextView) findViewById(R.id.text_match_address);
        textPrice = (TextView) findViewById(R.id.text_match_price);
        textTime = (TextView) findViewById(R.id.text_match_time);
        textNumber = (TextView) findViewById(R.id.text_match_number);
        textInfo = (TextView) findViewById(R.id.text_match_info);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        buttonMatchn = (Button) findViewById(R.id.button_matchn);
    }

    private void initEvents() {
        buttonMatchn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MatchDetailActivity.this, MatchnActivity.class);
                intent.putExtra("m_id",matchId);
                startActivity(intent);
            }
        });

        textPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + matchPhone));
                if (ActivityCompat.checkSelfPermission(MatchDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void showData() {
        matchId = getIntent().getIntExtra("m_id", -1);
        matchName = getIntent().getStringExtra("m_name");
        textName.setText(matchName);
        matchPicture = getIntent().getStringExtra("m_picture");
        Picasso.with(context)
                .load(matchPicture)
                .into(imageView);
        matchPhone = getIntent().getStringExtra("m_phone");
        textPhone.setText(matchPhone);
    }

    private void initData() {
        MatchModel matchModel = new MatchModel();
        matchModel.matchDetail(String.valueOf(matchId),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        MatchBean matchBean = (MatchBean) o;
        textInfo.setText(matchBean.getM_info());
        textAddress.setText(matchBean.getM_address());
        textPrice.setText(String.valueOf(matchBean.getM_price()));
        textTime.setText(matchBean.getM_time());
        textNumber.setText(String.valueOf(matchBean.getM_number()));
    }

    @Override
    public void onFail() {

    }
}
